/**
 * Copyright (C)  Gadglet .
 *
 * This file is part of Gadglet
 *
 * Gadglet is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Gadglet is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with Gadglet. If not, see <http://www.gnu.org/licenses/>.
 */

package com.gadglet.gadgets.personalNotes.client;

import com.gadglet.client.gwt.GadgetConfig;
import com.gadglet.client.gwt.ui.DebugDialogBox;
import com.gadglet.client.gwt.ui.SimpleDialogBox;
import com.gadglet.params.GadgetType;
import com.google.gwt.gadgets.client.Gadget;
import com.google.gwt.gadgets.client.Gadget.AllowHtmlQuirksMode;
import com.google.gwt.gadgets.client.Gadget.Content;
import com.google.gwt.gadgets.client.Gadget.InjectModulePrefs;
import com.google.gwt.gadgets.client.Gadget.UseLongManifestName;
import com.google.gwt.gadgets.client.NeedsViews;
import com.google.gwt.gadgets.client.PreferencesFeature;
import com.google.gwt.gadgets.client.PreferencesProvider;
import com.google.gwt.gadgets.client.ViewFeature;


/**
 * The Gadget "main" object. 
 * <p> See <a href='http://gwt-google-apis.googlecode.com/svn/javadoc/gadgets/1.2/index.html'>com.google.gwt.gadgets</a>
 * <p> See <a href='http://code.google.com/p/gwt-google-apis/wiki/GadgetsGettingStarted'>Gadget using GWT</a>

 * <p> NOTE: Google GWT Gadget (ver 1.2) has an issue with the use of "@InjectModulePrefs".
 * The Gadget manifest (xml file) is incorrectly formated. Developer must open the compiled manifest
 * and fix it manually, by removing additional unnecessary  &ltModulePrefs&gt .. &lt/ModulePrefs&gt
 * <p>In the case of "implements NeedsViews" the GWT compiler adds  additional unnecessary &ltRequire feature="views"/&gt
 *@see  com.google.gwt.gadgets.client.Gadget
 *
 */
@InjectModulePrefs(files = {"prefInject.txt"})
// Create a short manifest name (instead of prepending the package prefix)
@UseLongManifestName(false)
@AllowHtmlQuirksMode(false)
@Content(contents = { PersonalNotesHomeView.class, PersonalNotesCanvasView.class,PersonalNotesProfileView.class })
public class PersonalNotesGadget extends Gadget<PersonalNotesPreferences> implements
NeedsViews  {

	private ViewFeature gadgetViewFeature;
	
	public ViewFeature getGadgetViewFeature() {
		return gadgetViewFeature;
	}

	private DebugDialogBox debug = null;

	final protected DebugDialogBox errorNotifier = DebugDialogBox
	.getErrorNotifier();
	final PreferencesFeature  prefsUtils = PreferencesProvider.get();
	final SimpleDialogBox serverMessage = SimpleDialogBox
			.getMesseageDialogBox();

	final PersonalNotesHomeView home = new PersonalNotesHomeView();

	final PersonalNotesCanvasView canvas = new PersonalNotesCanvasView();
	
	// this one is for Gmail sidebar
	final PersonalNotesProfileView profile = new PersonalNotesProfileView();
	
	@Override
	protected void init(final PersonalNotesPreferences prefs) {
		GadgetConfig gc = GadgetConfig.getGadgetConfig();

		// this is a Minglet type of Gadget
		gc.setGadgetType(GadgetType.MINGLET);

		debug = DebugDialogBox.getErrorNotifier();
		/* NOTE: in some cases (SAP streamwork.com) gadgetViewFeature.getCurrentView() will return
		 "profile" as it is used as an alias for "home". 
		*/ 
		if (gadgetViewFeature == null) {
			errorNotifier.showError(7, "gadgetViewFeature is null");
			home.init(this.getGdget());
		} else if (gadgetViewFeature.getCurrentView().getName()
				.toUpperCase().equals("CANVAS"))
			canvas.init(this.getGdget());
		else if (gadgetViewFeature.getCurrentView().getName()
				.toUpperCase().equals("PROFILE"))
			profile.init(this.getGdget());
		else
			home.init(this.getGdget());
		
	}

	@Override
	public void initializeFeature(ViewFeature feature) {
		
		gadgetViewFeature = feature;

	}
	
	PersonalNotesGadget getGdget() {
		return this;
	}

}